package com.daniel.juc.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Executors 工具类，3大方法
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/5/21 10:20
 */
public class Demo01 {

	public static void main(String[] args) {
		// 单个线程池
		// ExecutorService pool = Executors.newSingleThreadExecutor();

		// 创建固定数量的线程池
		// ExecutorService pool = Executors.newFixedThreadPool(5);

		// 可伸缩的线程池
		ExecutorService pool = Executors.newCachedThreadPool();

		try {
			for (int i = 1; i <= 100; i++) {
				// 使用线程池之后，使用线程池来创建线程
				pool.execute(() -> {
					System.out.println(Thread.currentThread().getName() + " ok");
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 线程池用完，程序结束，关闭线程池
			pool.shutdown();
		}
	}
}
